'''
@Author: your name
@Date: 2020-02-05 20:51:45
@LastEditTime : 2020-02-05 21:05:52
@LastEditors  : Please set LastEditors
@Description: In User Settings 
@FilePath: \pyqt5\demo_100_QToolButton.py
'''
from PyQt5.Qt import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('demo_100_QToolButton')
        self.resize(500,500)
        self.setup_ui()

    def setup_ui(self):
        self.QToolButton_test()
    
    def QToolButton_test(self):
        btn = QToolButton(self)
        btn.setIcon(QIcon('ico/file.ico'))
        btn.triggered.connect(lambda: print('toolbutton is triggered...'))
        btn.clicked.connect(lambda: print('toolbutton is pressed...'))

        menu = QMenu(btn)

        sub_menu = QMenu(menu)
        sub_menu.setTitle('delete')
        menu.addMenu(sub_menu)

        menu.addSeparator()

        action1 = QAction(menu)
        action2 = QAction(menu)
        action1.setText('action1')
        action2.setText('action2')
        action2.triggered.connect(lambda: print('action2 is triggered'))
        menu.addAction(action1)
        menu.addAction(action2)

        btn.setToolTip('delete one file')
        btn.setPopupMode(QToolButton.MenuButtonPopup)
        btn.setMenu(menu)


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    win = Window()
    win.show()
    sys.exit(app.exec_())
